package com.iraeta.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.iraeta.api.HikApi;
import com.iraeta.api.WeiXinApi;
import com.iraeta.common.Result;
import com.iraeta.common.ServiceException;
import com.iraeta.dao.FangkerecordDao;
import com.iraeta.entity.Fangkerecord;
import com.iraeta.entity.ProcessStatus;
import com.iraeta.entity.ShenHeStatus;
import com.iraeta.service.FangKeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class FangKeServiceImpl implements FangKeService {
    @Autowired
    HikApi hikApi;
    @Autowired
    WeiXinApi weiXinApi;
    @Autowired
    FangkerecordDao fangkerecordDao;

    @Override
    public void getFangKeRecordFromHikApi() throws Exception {
        List<Fangkerecord> fangKeServiceList = hikApi.fangKeList();
        for (Fangkerecord fangkerecord : fangKeServiceList) {
            if(StrUtil.isEmpty(fangkerecord.getOrderid())){
                continue;
            }
            if(StrUtil.isEmpty(fangkerecord.getReceptionistid())){
                continue;
            }
            Fangkerecord fangkerecordDb = fangkerecordDao.selectByOrderId(fangkerecord.getOrderid());
            if(fangkerecordDb == null){
                fangkerecordDao.insert(fangkerecord);
            }
        }
    }

    @Override
    public void processFangKeRecord() {
        List<Fangkerecord> fangkerecordList = fangkerecordDao.listNeedProcessRecord();
        for (Fangkerecord fangkerecord : fangkerecordList) {
            fangkerecord.setProcessstatus(ProcessStatus.hasProcess);
            this.processFangKeOneRecord(fangkerecord);
            fangkerecordDao.updateByPrimaryKey(fangkerecord);
        }
    }
    private void processFangKeOneRecord(Fangkerecord fangkerecord) throws ServiceException {
        String orgIndexCode = "";
        try {
            orgIndexCode = hikApi.getRenYuanOrgIndexCode(fangkerecord.getReceptionistid());
        }catch (Exception e){
            fangkerecord.setProcesserror("orgIndexCode获取失败[" + e.getMessage() + "]");
            return;
        }
        List<String> orgShenHeWxList = null;
        try {
            orgShenHeWxList = hikApi.getOrgShenHeWx(orgIndexCode);
        }catch (Exception e){
            fangkerecord.setProcesserror("org微信list获取失败[" + e.getMessage() + "]");
            return;
        }
        try {
            StringBuilder toUser = new StringBuilder();
            for (String s : orgShenHeWxList) {
                toUser.append(s.substring(2)).append("|");
            }
            String toUserStr = toUser.substring(0, toUser.length() - 1);
            String content = "访客姓名:" + fangkerecord.getVisitorname() + "\n" +
                    "访客手机号:" + fangkerecord.getPhoneno() + "\n" +
                    "被访人:" + fangkerecord.getReceptionistname() + "\n" +
                    "拜访原因:" + fangkerecord.getVisitpurpose() + "\n" +
                    "<a href=\"http://124.128.207.66:8281/hikfangke/#/shenhe?id=" + fangkerecord.getOrderid() + "\">快捷审核</a>";
            weiXinApi.setWxUserTextMessage(toUserStr, content);
        }catch (Exception e){
            fangkerecord.setProcesserror("未知错误:" + e.getMessage());
        }
    }

    /*获取访客记录， 如果访客id存在并且记录是待审核的话， 否则返回已审核*/
    @Override
    public String getFangKeRecord(String orderId) {
        Fangkerecord fangkerecord = fangkerecordDao.selectByOrderId(orderId);
        if(fangkerecord == null){
            return JSON.toJSONString(Result.error("非法访客记录"));
        }
        if(fangkerecord.getShenhestatus() == ShenHeStatus.hasShenHe){
            return JSON.toJSONString(Result.error("访客申请已审核"));
        }else{
            return JSON.toJSONString(Result.success(fangkerecord));
        }
    }

    @Override
    public void shenHeFangKeRecord(String orderId, int status) {
        Fangkerecord fangkerecord = fangkerecordDao.selectByOrderId(orderId);
        if(fangkerecord == null){
            throw new ServiceException("非法访客记录");
        }
        if(fangkerecord.getShenhestatus() == ShenHeStatus.hasShenHe){
            throw new ServiceException("访客申请已审核");
        }
        try {
            fangkerecord.setShenhestatus(ShenHeStatus.hasShenHe);
            fangkerecord.setProcessstatus(ProcessStatus.hasProcess);
            fangkerecordDao.updateByPrimaryKey(fangkerecord);
            hikApi.shenHeRecord(fangkerecord.getInvoiceno(), status);
        }catch (Exception e){
            fangkerecord.setProcesserror(e.getMessage());
            fangkerecordDao.updateByPrimaryKey(fangkerecord);
            throw new ServiceException("未知错误:" + e.getMessage());
        }
    }

    @Override
    public void deleteHisFangKeRecord() {
        String dateStr = DateUtil.formatDate(DateUtil.offsetDay(new Date(), -3));
        fangkerecordDao.deleteRecordByDay(dateStr);
    }

    @Override
    public void updateAllRecordStatus() {
        fangkerecordDao.updateAllRecordStatus();
    }
}
